<template lang="pug">
  keep-alive
    kalix-table(bizKey="examineeRoom" title="考生列表"
      v-bind:targetURL="targetURL" v-bind:tableFields="tableFields" bizSearch="ExamineeRoomSearch"
      v-bind:btnList="btnList" v-bind:bizDialog="bizDialog" v-bind:toolbarBtnList="toolbarBtnList"
      v-bind:customToolBar="customToolBar" v-on:afterDialogClose="dialogClose" v-bind:dialogOptions="dialogOptions"
      ref="examineeRoomTable")
</template>

<script type="text/ecmascript-6">
  import {ExamineeRoomsURL, ExamineeRoomsExportURL, ExamineeUserPasswordURL, ExamControlSheetExportURL, ExamNumberCardInfoExportURL} from '../config.toml'
  import {ExamineeRoomBtnList} from './config'
  import FormModel from './examineeRoomModel'

  export default {
    name: 'kalix-exam-examineeroommaintain',
    data() {
      return {
        targetURL: ExamineeRoomsURL,
        resetPasswordURL: ExamineeUserPasswordURL,
        btnList: ExamineeRoomBtnList,
        tableFields: [
          {prop: 'examRoom', label: '考场'},
          {prop: 'examRoomNo', label: '考场坐号'},
          {prop: 'name', label: '考生'},
          {prop: 'examCardNumber', label: '准考证号'},
          {prop: 'idCards', label: '身份证号'},
          {prop: 'state', label: '参考状态'}
        ],
        bizDialog: [
          {id: 'edit', dialog: 'ExamineeRoomEdit'},
          {id: 'view', dialog: 'ExamineeRoomView'},
          {id: 'importExcel', dialog: 'ExamineeRoomImport'},
          {id: 'addExaminee', dialog: 'ExamineeRoomAdd'}
        ],
        toolbarBtnList: [
          {id: 'add', isShow: false},
          {id: 'importExcel', title: '考场信息导入', isShow: true, icon: 'icon-add'},
          {id: 'exportExcel', title: '导出', isShow: true, icon: 'icon-add'},
          {id: 'resetPassword', title: '考生密码重置', isShow: true, icon: 'icon-add'},
          {id: 'addExaminee', title: '考生添加', isShow: true, icon: 'icon-add'},
          {id: 'examControlSheet', title: '考场对照单', isShow: true, icon: 'icon-add'},
          {id: 'examNumberCardInfo', title: '准考证数据表', isShow: true, icon: 'icon-add'}
        ],
        dialogOptions: {}
      }
    },
    methods: {
      customToolBar(btnId, table) {
        switch (btnId) {
          case 'importExcel': {
            this.onImportExcel(table)
            break
          }
          case 'exportExcel': {
            this.onExportExcel(table)
            break
          }
          case 'resetPassword': {
            this.onResetPassword(table)
            break
          }
          case 'addExaminee': {
            this.onAddExaminee(table)
            break
          }
          case 'examControlSheet': {
            this.exportExamControlSheet(table)
            break;
          }
          case 'examNumberCardInfo': {
            this.exportExamNumberCardInfo(table)
            break;
          }
        }
      },
      exportExamNumberCardInfo(table) {
        this.exportExcelInfo(table, ExamNumberCardInfoExportURL, '准考证数据表.xlsx')
      },
      exportExamControlSheet(table) {
        this.exportExcelInfo(table, ExamControlSheetExportURL, '考场对照单.xlsx')
      },
      onAddExaminee(table) {
        let queryParams = table.getQueryParams()
        let subjectCode = null;
        let dateBegin = null;
        if (queryParams != undefined && queryParams.length > 0) {
          let queryObj = JSON.parse(queryParams)
          subjectCode = queryObj.subjectVal
          dateBegin = queryObj.dateBegin
          if (subjectCode == undefined || subjectCode.length == 0
            || dateBegin == undefined || dateBegin.length == 0) {
            this.$KalixMessage.error('请先查询出数据在做添加考生操作！')
            return
          }
        } else {
          this.$KalixMessage.error('请先查询出数据在做添加考生操作！')
          return
        }
        let dig =
          table.bizDialog.filter((item) => {
            return item.id === 'addExaminee'
          })
        table.whichBizDialog = dig[0].dialog
        setTimeout(() => {
          FormModel.subjectCode = subjectCode
          FormModel.dateBegin = dateBegin
          table.$refs.kalixDialog.$refs.kalixBizDialog.open('考生添加', false, FormModel)
        }, 20)
      },
      onResetPassword(table) {
        let queryParams = table.getQueryParams()
        this.$confirm('确定要重置密码吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          return this.axios.request({
            method: 'POST',
            url: this.resetPasswordURL,
            params: {
              jsonStr: queryParams
            }
          })
        }).then(response => {
          this.$KalixMessage.success(response.data.msg)
        })
      },
      onImportExcel(table) {
        let dig =
          table.bizDialog.filter((item) => {
            return item.id === 'importExcel'
          })
        table.whichBizDialog = dig[0].dialog
        setTimeout(() => {
          table.$refs.kalixDialog.$refs.kalixBizDialog.open('批量导入', false, '')
        }, 20)
      },
      dialogClose() {
        this.$refs.examineeRoomTable.refresh()
      },
      onExportExcel(table) {
        this.exportExcelInfo(table, ExamineeRoomsExportURL, '考生信息.xlsx')
      },
      exportExcelInfo(table, url, fileName) {
        let queryParams = table.getQueryParams()
        if (queryParams != undefined && queryParams.length > 0) {
          let queryObj = JSON.parse(queryParams)
          let subjectCode = queryObj.subjectVal
          let dateBegin = queryObj.dateBegin
          if (subjectCode == undefined || subjectCode.length == 0
            || dateBegin == undefined || dateBegin.length == 0) {
            this.$KalixMessage.error('请先选择考试科目及考试时间！')
            return
          }
        } else {
          this.$KalixMessage.error('请先查询出数据在做导出操作！')
          return
        }

        const loading = this.$loading({
          lock: true,
          text: 'Excel数据导出中...',
          spinner: 'el-icon-loading',
          background: 'rgba(255, 255, 255, 0.7)'
        })

        this.axios.request({
          headers: {
            'Content-Type': 'application/x-www-form-urlencoded'
          },
          method: 'get',
          baseURL: '../api',
          url: url,
          responseType: 'blob',
          params: {
            jsonStr: queryParams
          }
        }).then(response => {
          if (response.data) {
            this.download(response.data, fileName)
            this.$KalixMessage.success('导出成功！')
          } else {
            this.$KalixMessage.error('导出失败！')
          }
          loading.close();
        }).catch((e) => {
          this.$KalixMessage.error('导出失败！原因：' + e.message)
          loading.close();
        })
      },
      download(data, fileName) {
        let url = window.URL.createObjectURL(new Blob([data]))
        let link = document.createElement('a')
        link.style.display = 'none'
        link.href = url
        link.setAttribute('download', fileName)
        document.body.appendChild(link)
        link.click()
      }
    }
  }
</script>

<style scoped lang="stylus" type="text/stylus">

</style>
